import { reqGetUserAddressListData, reqGetTradeListData } from '../../api';

const state = {
    userAddressList: [],
    tradeInfo: {},
};

const actions = {
    async getUserAddressList({ commit }) {
        const result = await reqGetUserAddressListData();
        if (result.code === 200) {
            commit('SAVE_ADDRESS_LIST_DATA', result.data);
        } else {
            console.log(result.message);
        }
    },
    async getTradeListData({ commit }) {
        const result = await reqGetTradeListData();
        if (result.code === 200) {
            commit('SAVE_TRADE_LIST_DATA', result.data);
        } else {
            console.log(result.message);
        }
    },
};

const mutations = {
    SAVE_ADDRESS_LIST_DATA(state, userAddressList) {
        state.userAddressList = userAddressList;
    },
    SAVE_TRADE_LIST_DATA(state, tradeInfo) {
        state.tradeInfo = tradeInfo;
    },
};

const getters = {
    // 商品列表
    detailArrayList(state) {
        return state.tradeInfo.detailArrayList;
    },
    // 商品总金额
    originalTotalAmount() {
        return state.tradeInfo.originalTotalAmount;
    },
    // 实付金额
    totalAmount() {
        return state.tradeInfo.totalAmount;
    },
    // 商品数量
    totalNum() {
        return state.tradeInfo.totalNum;
    },
    // 订单编号
    tradeNo() {
        return state.tradeInfo.tradeNo;
    },
};

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
};
